package com.yike.product.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yike.common.exception.BusinessException;
import com.yike.common.tool.LoginUtil;
import com.yike.product.entity.GoodsSupplier;
import com.yike.product.mapper.GoodsSupplierMapper;
import com.yike.product.param.GoodsSupplierParam;
import com.yike.product.service.GoodsSupplierService;
import com.yike.product.param.GoodsSupplierPageParam;
import com.yike.common.base.BaseServiceImpl;
import com.yike.common.pagination.Paging;
import com.yike.common.pagination.PageInfo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yike.product.vo.GoodsSupplierVo;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 商品供应商 服务实现类
 *
 * @author Sakura
 * @since 2024-10-21
 */
@Slf4j
@Service
public class GoodsSupplierServiceImpl extends BaseServiceImpl<GoodsSupplierMapper, GoodsSupplier> implements GoodsSupplierService {

    @Autowired
    private GoodsSupplierMapper goodsSupplierMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveGoodsSupplier(GoodsSupplierParam goodsSupplierParam) throws Exception {
        // 先判断供应商名称是否重复
        GoodsSupplier goodsSupplier = goodsSupplierMapper.selectOne(Wrappers.lambdaQuery(GoodsSupplier.class)
                .eq(GoodsSupplier::getName, goodsSupplierParam.getName()));
        if (goodsSupplier != null) {
            throw new BusinessException("当前供应商已存在，请勿重复添加");
        }
        goodsSupplier = new GoodsSupplier();
        BeanUtils.copyProperties(goodsSupplierParam, goodsSupplier);
        goodsSupplier.setCreateBy(LoginUtil.getUserId());
        return super.save(goodsSupplier);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateGoodsSupplier(GoodsSupplierParam goodsSupplierParam) throws Exception {
        // 先获取供应商信息
        GoodsSupplier goodsSupplier = goodsSupplierMapper.selectById(goodsSupplierParam.getId());
        if (goodsSupplier == null) {
            throw new BusinessException("供应商信息异常");
        }
        // 判断修改名称是否已存在
        Integer checkNum = goodsSupplierMapper.selectCount(Wrappers.lambdaQuery(GoodsSupplier.class)
                .eq(GoodsSupplier::getName, goodsSupplierParam.getName())
                .ne(GoodsSupplier::getId, goodsSupplierParam.getId()));
        if (checkNum > 0) {
            throw new BusinessException("当前供应商已存在，请勿重复添加");
        }
        BeanUtils.copyProperties(goodsSupplierParam, goodsSupplier);
        goodsSupplier.setUpdateBy(LoginUtil.getUserId());
        return super.updateById(goodsSupplier);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteGoodsSupplier(Long id) throws Exception {
        return super.removeById(id);
    }

    @Override
    public GoodsSupplierVo getGoodsSupplier(Long id) throws Exception {
        return goodsSupplierMapper.getGoodsSupplierVo(id);
    }

    @Override
    public Paging<GoodsSupplierVo> getGoodsSupplierPageList(GoodsSupplierPageParam goodsSupplierPageParam) throws Exception {
        Page<GoodsSupplier> page = new PageInfo<>(goodsSupplierPageParam);
        IPage<GoodsSupplierVo> iPage = goodsSupplierMapper.getGoodsSupplierList(page, goodsSupplierPageParam);
        return new Paging<GoodsSupplierVo>(iPage);
    }

}
